#!/usr/bin/env bash


# ----------------------------------------------------------------------
# Filename	:  ipHosts
# Version	:  1.0
# Date		:  2022/02/18
# Author	:  Lz
# Email		:  liuzuo@kylinos.com.cn 
# History	:     
#            	   Version 1.0, 2022/02/18
# Function	:  远程IP访问限制 
# Out		:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------


# 测试主题
Title_Env_LTFLIB="安全加固(接入类) - 远程IP访问限制"


## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB(){
        return $TPASS
}


## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB(){
        return $TPASS
}


## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB(){
        testcase_1
        testcase_2

        return $TPASS
}

# 允许远程访问的IP列表
testcase_1(){
        local flag_str="^sshd:"
	
        # 查看文件中配置
        local file_conf="/etc/hosts.allow"
        local cmd="cat ${file_conf}"
	if [ "Z${Allow_Access_SSRFlag}" == "Z" ];then
		eval ${cmd} | grep "$flag_str"
		CommRetParse_LTFLIB "${cmd}查询,要求不存在${flag_str}" "False" "yes"
	else
       		# 错误选项
       		local false_str="${flag_str}"
       		# 正确选项
       		local true_str="${flag_str} *${Allow_Access_SSRFlag}$"

       		eval ${cmd} | grep "$flag_str"
       		eval ${cmd} | grep -q "${true_str}"
       		CommRetParse_LTFLIB "${cmd}查询,要求${true_str}" "False" 

       		eval ${cmd} | grep -v "${true_str}" | grep "${false_str}"
       		CommRetParse_LTFLIB "${cmd}查询,不存在'${true_str}'以外的值" "False" "yes"
	fi
}


# 禁止远程访问的IP列表
testcase_2(){
        local flag_str="^sshd:"
	
        # 查看文件中配置
        local file_conf="/etc/hosts.deny"
        local cmd="cat ${file_conf}"
	if [ "Z${Deny_Access_SSRFlag}" == "Z" ];then
		eval ${cmd} | grep "$flag_str"
		CommRetParse_LTFLIB "${cmd}查询,要求不存在${flag_str}" "True" "yes"
	else
       		# 错误选项
       		local false_str="${flag_str}"
       		# 正确选项
       		local true_str="${flag_str} *${Deny_Access_SSRFlag}$"

       		eval ${cmd} | grep "$flag_str"
       		eval ${cmd} | grep -q "${true_str}"
       		CommRetParse_LTFLIB "${cmd}查询,要求${true_str}"

       		eval ${cmd} | grep -v "${true_str}" | grep "${false_str}"
       		CommRetParse_LTFLIB "${cmd}查询,不存在'${true_str}'以外的值" "False" "yes"
	fi
}


#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
